﻿'use strict';

app.controller('BannerController', function($scope, $resource,$stateParams,$modal,$state) {
    //查询
    $scope.query = function(page,filter){
        var $com = $resource($scope.app.host + "/banner/?page=:page&search=:filter",{page:'@page',filter:'@filter'});
        if(!page){
            page=1;
        }else{
            page=parseInt(page);
        }
        $com.get({page:page,filter:filter},function(data){
            //扩展分页数据，显示页签，最终效果为  < 1 2 3 4 5 >
            data.data.page_index = page;
            data.data.pages = [];    //页签表
            var N = 5;          //每次显示5个页签
            if(N > data.data.page.pages){
                N = data.data.page.pages;
            }
            var s = Math.floor(page/N)*N;
            if(s==page)s-=N;
            s += 1;
            var e = Math.min(data.data.page.count,s+N-1)
            for(var i=s;i<=e;i++)
                data.data.pages.push(i)
            $scope.data = data.data;
            $scope.search_context = filter;
        });
    }
    //搜索跳转
    $scope.search = function(){
        $state.go('app.banner.list',{search:$scope.search_context});
    }
    //全选
    var selected = false;
    $scope.selectAll = function(){
        selected = !selected;
        angular.forEach($scope.data.results,function(item){
            item.selected = selected;
        });
    }
    //根据url参数（分页、搜索关键字）查询数据
    $scope.query($stateParams.page,$stateParams.search);
});

app.controller('ConfirmController', ['$scope', '$modalInstance', function($scope, $modalInstance){
     $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
  };
}]);

app.controller('BannerDetailController', function($rootScope,$scope, $resource, $stateParams,$state,$http) {
  $scope.edit_mode = !!$stateParams.id;
  if($scope.edit_mode){
      var $com = $resource($scope.app.host + "/banner/getOne/:id/?",{id:'@id'});
      var resp = $com.get({id:$stateParams.id},function(data){
          $scope.data = data.data;
      });
  }
  else{
      $scope.data = {};
  }
  $scope.submit = function(){
      if($scope.edit_mode){
          var $com = $resource($scope.app.host + "/banner/update/:id/?",{id:'@id'},{
              'update': { method:'PUT' },
          });
          $com.update({id:$stateParams.id},$scope.data,function(data){
              $state.go($rootScope.previousState,$rootScope.previousStateParams);
          });
      }
      else{
          var $com = $resource($scope.app.host + "/banner/save/?");
          $com.save($scope.data,function(data){
              $state.go('app.banner.list');
          });
      }
  };
  $scope.delete = function(){
      var $com = $resource($scope.app.host + "/banner/delete/:id/?",{id:'@id'});
      $com.delete({id:$stateParams.id},function(){
          $state.go('app.banner.list');
      })
  }

    $scope.img_upload = function(files) {       //单次提交图片的函数
        var data = new FormData();      //以下为像后台提交图片数据
        data.append('img', files[0]);
        $http({
            method: 'post',
            url: $scope.app.host + '/upload/images',
            data:data,
            headers: {'Content-Type': undefined},
            transformRequest: angular.identity
        }).success(function(data) {
            $scope.data.imageUrl = data.data;
        })
    };
});